package com.csdj.middleware.config;


import com.csdj.common.constant.CacheConstant;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;


@Configuration
public class CacheConfig {
    @Bean
    @ConditionalOnBean(RedisConnectionFactory.class)
    public CacheManager cacheManager(RedisConnectionFactory factory, Jackson2JsonRedisSerializer serializer){
        //创建一个RedisCache的配置对象
        //过期时间设置为一天
        //对存的的key,value进行序列化
        RedisCacheConfiguration config=RedisCacheConfiguration.defaultCacheConfig();
        config=config.entryTtl(Duration.ofDays(1));
        config=config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        config=config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));
        config=config.disableCachingNullValues();

        //过期时间设置为30天
        //对存的的key,value进行序列化
        RedisCacheConfiguration userConfig=RedisCacheConfiguration.defaultCacheConfig();
        userConfig=userConfig.entryTtl(Duration.ofDays(30));
        userConfig=userConfig.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        userConfig=userConfig.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));
        userConfig=userConfig.disableCachingNullValues();

        //过期时间设置为12小时， 方便更新
        //对菜单进行缓存
        RedisCacheConfiguration menuConfig=RedisCacheConfiguration.defaultCacheConfig();
        userConfig=userConfig.entryTtl(Duration.ofHours(12));
        userConfig=userConfig.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        userConfig=userConfig.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));
        userConfig=userConfig.disableCachingNullValues();

        //对缓存空间设置单独的配置，在这个可根据业务，如果数据很久才会变一次时间就可设成永久
        Map<String,RedisCacheConfiguration> configMap=new HashMap<>();
        configMap.put(CacheConstant.CACHE_CONFIG,config);
        configMap.put(CacheConstant.CACHE_USER,userConfig);
        configMap.put(CacheConstant.CACHE_MENU,menuConfig);
        //创建RedisCacheManage对象，将上面的配置导入
        RedisCacheManager redisCacheManager=RedisCacheManager.builder(factory)
                .withInitialCacheConfigurations(configMap)
                .build();
        return redisCacheManager;
    }

    @Bean("keyGenerator")
    public KeyGenerator keyGenerator(){
        return (o,m,objs)->{
            StringBuilder sb = new StringBuilder();
            sb.append(m.getName());
            if(objs != null && objs.length>0){
                for(Object arg: objs){
                    sb.append(arg.toString());
                }
            }
            return sb.toString();
        };

    }
}
